package cn.com.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.api.entity.ApiU_match_works;
import cn.com.api.service.ApiU_match_worksService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "u_match_works/")
public class ApiU_match_worksController {
	@Autowired
	private ApiU_match_worksService apiU_match_worksService;

	private static Logger logger = Logger.getLogger(ApiU_match_worksController.class);

	@Autowired
	private CheckService checkService;

	/**
	 * 根据赛事ID报名
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("insertU_match-works")
	public void insertMatchworks(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			ApiU_match_works u_match_works) {

		Map<String, Object> map = new HashMap<String, Object>();
		try {

			if (checkService.checkToken(user_id, token, map)) {
				apiU_match_worksService.insertMatchworks(u_match_works);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "报名成功！");
			}

		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));

	}

	/**
	 * 根据赛事ID获取投票作品列表
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "findU_match_votelist")
	public void findU_match_vote(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token) {

		Map<String, Object> map = new HashMap<String, Object>();
		try {

			String type = request.getParameter("type");
			String match_id = request.getParameter("match_id");
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("user_id", user_id);
			params.put("type", type);
			params.put("token", token);
			params.put("match_id", match_id);
			if (checkService.checkToken(user_id, token, map)) {
				// 根据赛事ID获取投票作品列表
				Map<String, Object> detailMap = new HashMap<String, Object>();
				List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();

				List<ApiU_match_works> list = apiU_match_worksService.findU_match_search_works(params);

				for (ApiU_match_works apiU_match_works : list) {
					detailMap = new HashMap<String, Object>();

					detailMap.put("works_id", apiU_match_works.getWorks_id());
					detailMap.put("works_number", apiU_match_works.getExt2());
					detailMap.put("join_name", apiU_match_works.getJoin_name());
					detailMap.put("works_url", apiU_match_works.getWorks_url());
					detailMap.put("vote_status", apiU_match_works.getRanking());
					listMap.add(detailMap);
				}

				map.put("datas", listMap);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "查询成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 根据选手姓名作品获取作品列表
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "findU_match_search_votelist")
	public void findU_match_search_votelist(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token) {

		Map<String, Object> map = new HashMap<String, Object>();
		try {

			String type = request.getParameter("type");
			String match_id = request.getParameter("match_id");
			String join_name = request.getParameter("join_name");
			Map<String, Object> params = new HashMap<String, Object>();

			params.put("user_id", user_id);
			params.put("type", type);
			params.put("token", token);
			params.put("match_id", match_id);
			params.put("join_name", join_name);
			if (checkService.checkToken(user_id, token, map)) {
				// 根据赛事ID,关键字获取投票作品列表
				Map<String, Object> detailMap = new HashMap<String, Object>();
				List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
				List<ApiU_match_works> list = apiU_match_worksService.findU_match_search_works(params);
				for (ApiU_match_works apiU_match_works : list) {
					detailMap = new HashMap<String, Object>();

					detailMap.put("works_id", apiU_match_works.getWorks_id());
					detailMap.put("works_number", apiU_match_works.getExt2());
					detailMap.put("join_name", apiU_match_works.getJoin_name());
					detailMap.put("works_url", apiU_match_works.getWorks_url());
					detailMap.put("vote_status", apiU_match_works.getRanking());
					listMap.add(detailMap);
				}
				map.put("datas", listMap);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "查询成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 根据赛事ID获取已经投票列表
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "findU_match_already_votelist")
	public void findU_match_already_vote(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token) {

		Map<String, Object> map = new HashMap<String, Object>();
		try {

			String type = request.getParameter("type");
			String match_id = request.getParameter("match_id");
			Map<String, Object> params = new HashMap<String, Object>();

			params.put("user_id", user_id);
			params.put("type", type);
			params.put("token", token);
			params.put("match_id", match_id);
			if (checkService.checkToken(user_id, token, map)) {

				Map<String, Object> detailMap = new HashMap<String, Object>();
				List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
				// 根据赛事ID获取已经投票列表
				List<ApiU_match_works> list = apiU_match_worksService.findU_match_already_works(params);
				for (ApiU_match_works apiU_match_works : list) {
					detailMap = new HashMap<String, Object>();

					detailMap.put("works_id", apiU_match_works.getWorks_id());
					detailMap.put("works_number", apiU_match_works.getExt2());
					detailMap.put("join_name", apiU_match_works.getJoin_name());
					detailMap.put("works_url", apiU_match_works.getWorks_url());
					detailMap.put("vote_status", apiU_match_works.getRanking());
					listMap.add(detailMap);
				}

				map.put("datas", listMap);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "查询成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 获取作品的信息，包含投票信息
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "findMatchworkInfo")
	@Transactional

	public @ResponseBody Map<String, Object> findMatchworkInfo(HttpServletRequest request, HttpServletResponse response,
			String user_id, String token) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("user_id", user_id);
			params.put("token", token);
			if (checkService.checkToken(user_id, token, map)) {
				String works_id = request.getParameter("works_id");
				Map<String, Object> result = apiU_match_worksService.findMatchworkInfo(Integer.valueOf(works_id));
				map.clear();
				map.put(Utils.DATAS, result);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.clear();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		return map;
	}

}
